import java.io.File;
import java.sql.Date;
import java.util.Random;
import java.sql.Connection;

import common.NoRepeatRandom;
import common.SQLCall;

public class ApeProblem
{
	private final static int MINPOINTS = 5;
	private final static int WEIGHT = 4;
	private final static int NUMOFCATEGORIES = 4;
	private File path;
	private static final Random rnd = new Random();
	private static NoRepeatRandom nrrnd = new NoRepeatRandom();
	private int category = -1;

	private final static int NUMTYPESOFFILES = 6;
	public final static int PINPUT = 0, POUTPUT = 1, TINPUT = 2;
	public final static int TOUTPUT = 3, CRITERIA = 4, INSTR = 5;

	String title;
	File[] file = new File[NUMTYPESOFFILES];

	public ApeProblem(File path)
	{
		this.path = path;
	}

	public SourceIterator getSource()
	{
		return new SourceIterator(path);
	}

	public int getPoints(Connection conn)
	{
		int total = SQLCall.getCategoryPoints(conn, getCategory());
		int num;
		int count;
		int result;

		count = total/MINPOINTS;
		num = rnd.nextInt(count + count/WEIGHT) +1;

		if(num > count) 
			num = num - count/WEIGHT;

		result = num * MINPOINTS;

		return result;

	}

	public Date getDateCreated()
	{
		long d = (new SourceIterator(path)).next().lastModified();

		return new Date(d);
	}

	public String getInputFileName()
	{
		if (file[TINPUT] == null)
			return null;

		return file[TINPUT].getName();
	}

	public int getCategory()
	{
		while(category == -1) {
			try
			{
				category = (1 + nrrnd.getNextInt(NUMOFCATEGORIES));
			} 
			catch (Exception e)
			{
				nrrnd = new NoRepeatRandom();
			}
		}
		return category;
	}
}
